package com.xbongbong.paas.pojo.dto;

import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.TemplateErrorCodeEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 处理应收款/付款计划的权限对应实体
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020-03-14 13:59
 */
@Setter
@Getter
@ToString
public class PlanFieldAttrDTO {
    /**
     * 是否有计划权限（付款计划/应收款）
     */
    private Boolean hasPlanPerm = true;
    /**
     * 是否有回款单/付款单权限
     */
    private Boolean hasSheetPerm = true;
    /**
     * 是否有核销预收款/核销预付款权限
     */
    private Boolean hasWriteOffPerm = true;
    /**
     * 关联回款单/付款单业务
     */
    private XbbRefTypeEnum linkSheetBusinessTypeEnum;
    /**
     * 关联应收款/付款计划业务
     */
    private XbbRefTypeEnum linkPlanBusinessTypeEnum;
    /**
     * 根据用户权限判断是否需要统计金额
     *
     * @param userVO
     * @param explainMap
     * @param xbbRefTypeEnum
     * @throws XbbException
     */
    public PlanFieldAttrDTO(UserVO userVO, Map<String, FieldAttrEntity> explainMap, XbbRefTypeEnum xbbRefTypeEnum) throws XbbException {
        FieldAttrEntity addPlanFieldAttr;
        FieldAttrEntity addWriteOffPreFieldAttr;
        FieldAttrEntity addSheetFieldAttr;
        String sheetAlias ;
        String planAlias ;
        switch (xbbRefTypeEnum) {
            case CONTRACT:
                addPlanFieldAttr = explainMap.get(ContractEnum.ADD_PAYMENT.getAttr());
                addWriteOffPreFieldAttr = explainMap.get(ContractEnum.ADD_WRITE_OFF_PREPAYMENT.getAttr());
                addSheetFieldAttr = explainMap.get(ContractEnum.ADD_PAYMENT_SHEET.getAttr());
                sheetAlias = ProPermissionAliasEnum.PAYMENT_SHEET_VIEW.getAlias();
                planAlias = ProPermissionAliasEnum.PAYMENT_VIEW.getAlias();
                linkSheetBusinessTypeEnum = XbbRefTypeEnum.PAYMENT_SHEET;
                linkPlanBusinessTypeEnum = XbbRefTypeEnum.PAYMENT;
                break;
            case PURCHASE:
                addPlanFieldAttr = explainMap.get(PurchaseEnum.ADD_PAYPLAN.getAttr());
                addWriteOffPreFieldAttr = explainMap.get(PurchaseEnum.ADD_WRITE_OFF_PREPAY.getAttr());
                addSheetFieldAttr = explainMap.get(PurchaseEnum.ADD_PAY_SHEET.getAttr());
                sheetAlias = ProPermissionAliasEnum.PAY_SHEET_VIEW.getAlias();
                planAlias = ProPermissionAliasEnum.PAY_PLAN_VIEW.getAlias();
                linkSheetBusinessTypeEnum = XbbRefTypeEnum.PAY_SHEET;
                linkPlanBusinessTypeEnum = XbbRefTypeEnum.PAY_PLAN;
                break;
            default:
                throw new XbbException(TemplateErrorCodeEnum.API_ERROR_350006);
        }
        if ( !userVO.getPermSet().contains(sheetAlias) || (userVO.getPermSet().contains(sheetAlias) && (Objects.isNull(addWriteOffPreFieldAttr) || Objects.equals(addWriteOffPreFieldAttr.getIsOpen(), 0))) ) {
            hasWriteOffPerm = false;
        }
        if ( !userVO.getPermSet().contains(sheetAlias) || (userVO.getPermSet().contains(sheetAlias) && (Objects.isNull(addSheetFieldAttr) || Objects.equals(addSheetFieldAttr.getIsOpen(), 0))) ) {
            hasSheetPerm = false;
        }
        if ( !userVO.getPermSet().contains(planAlias) || (userVO.getPermSet().contains(planAlias) && (Objects.isNull(addPlanFieldAttr) || Objects.equals(addPlanFieldAttr.getIsOpen(), 0))) ) {
            hasPlanPerm = false;
        }
    }
}
